package com.zmc.lostfound.search.listener;

import com.zmc.lostfound.search.service.PostSearchService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import static com.zmc.lostfound.common.constant.RabbitMqConstant.*;

/**
 * 用于监听消息队列中的消息
 *
 * @author ZMC
 */
@Component
public class RabbitPostListener {

    private final PostSearchService postSearchService;

    public RabbitPostListener(PostSearchService postSearchService) {
        this.postSearchService = postSearchService;
    }

    /**
     * 监听post新增/修改的业务
     *
     * @param id 主键id
     */
    @RabbitListener(queues = {POST_SAVE_QUEUE})
    public void listenUserSave(Long id) {
        // 根据id新增或修改ES的user索引库的文档
        postSearchService.saveById(id);
    }

    /**
     * 监听post删除的业务
     *
     * @param id 主键id
     */
    @RabbitListener(queues = {POST_DELETE_QUEUE})
    public void listenUserDelete(Long id) {
        // 根据id删除ES的user索引库的文档
        postSearchService.deleteById(id);
    }

}